package com.jic.member.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MememberIntegralRule;
import com.jic.member.impl.MememberIntegralGiveConsumeServiceImpl;
import com.jic.member.impl.MememberIntegralRuleServiceImpl;
import com.jic.member.request.EditMememberIntegralRuleRequest;
import com.jic.member.request.MememberIntegralRuleRequest;
import com.jic.member.request.MememberIntegralRuleUpdateRequest;
import com.jic.member.response.MememberIntegralRuleResponse;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 会员积分管理-积分规则列表
 * @author yinmenghui
 */

@Slf4j
@RestController
@RequestMapping("/meMemberIntegralRule")
public class MememberIntegralRuleController {

    private final MememberIntegralRuleServiceImpl service;
    public MememberIntegralRuleController(MememberIntegralRuleServiceImpl service, MememberIntegralGiveConsumeServiceImpl mememberIntegralGiveConsumeServiceImpl) {
        this.service = service;
    }

    /**
     *
     * @param form
     * @return
     */

    @ApiOperation(value = "获取积分规则列表")
    @ResponseBody
    @RequestMapping(value = "/queryList")
    public RestResult<PageResult<MememberIntegralRuleResponse>> queryList(@RequestBody MememberIntegralRuleRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MememberIntegralRule mememberIntegralRule = BeanUtil.copy(form, MememberIntegralRule.class);
        PageResult<MememberIntegralRule> result = service.queryPage(mememberIntegralRule, page);
            List<MememberIntegralRuleResponse> list = result.getRows().stream().map(temp -> {
            MememberIntegralRuleResponse mememberIntegralRuleResponse = BeanUtil.copy(temp, MememberIntegralRuleResponse.class);
            return mememberIntegralRuleResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MememberIntegralRuleResponse> pageResult = new PageResult<>();
        pageResult.setRows(list);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    /**
     * 修改会员积分状态
     * @param form
     * @return
     */
    @Transactional(rollbackFor = Exception.class )
    @ApiOperation(value = "修改会员积分状态/删除标示")
    @ResponseBody
    @RequestMapping("/updateStatus")
    public RestResult  updateStatus(@RequestBody MememberIntegralRuleUpdateRequest form) {
        RestResult restResult = new RestResult();
        log.info("修改会员积分状态或删除标示=========》,{}", form.toString());
        final Long update = service.updateStatus(form);
        if(update >0){
            restResult.setCode("0000");
        restResult.setData(update);
        }
        return restResult;
    }


    /**
     * stopStatus
     * @param form
     * @return
     */
    @Transactional(rollbackFor = Exception.class )
    @ApiOperation(value = "修改会员积分状态/删除标示")
    @ResponseBody
    @RequestMapping("/stopStatus")
    public RestResult  stopStatus(@RequestBody MememberIntegralRuleUpdateRequest form) {
        RestResult restResult = new RestResult();
        log.info("修改会员积分状态或删除标示=========》,{}", form.toString());
        restResult = service.stopStatus(form);
        return restResult;
    }


    @ApiOperation("新增积分获取方式信息")
    @RequestMapping("addIntegraRuleInfo")
    public RestResult  addIntegraRuleInfo(@RequestBody MememberIntegralRule request){
        log.info("新增积分获取方式信息=========》,{}", request.toString());
        RestResult restResult = service.addIntegraRuleInfo(request);
        return restResult;
    }
    @ApiOperation("查询积分规则信息")
    @RequestMapping("queryMemberIntegralInfo")
    public RestResult  queryMemberIntegralInfo(@RequestBody MememberIntegralRuleRequest request){
        log.info("查询积分规则信息参数=========》,{}", request.toString());
        MememberIntegralRuleResponse mememberIntegralRuleResponse = service.queryMemberIntegralInfo(request);
        return RestResult.success(mememberIntegralRuleResponse);
    }

    @ApiOperation("编辑积分信息")
    @RequestMapping("editMemberIntegraInfo")
    public RestResult  editMemberIntegraInfo(@RequestBody EditMememberIntegralRuleRequest request){
        RestResult restResult = new RestResult<>();
        try {
            log.info("查询积分规则信息参数=========》,{}", request.toString());
             restResult = service.editMemberIntegraInfo(request);
        }catch (Exception e){
            restResult.setMsg(e.getMessage());
        }
        return restResult;
    }


    /**
     * 删除积分信息
     * @param form
     * @return
     */
    @ApiOperation(value="删除积分信息", notes="删除积分信息")
    @ResponseBody
    @RequestMapping("/deletMemberIntegralInfo")
    public RestResult deletMemberIntegralInfo(@RequestBody MememberIntegralRuleUpdateRequest form) {
        RestResult restResult = new RestResult<>();
        try {
            if (form.getRuleId() !=null){
                restResult=service.deletMemberIntegralInfo(form);
            }
        } catch (Exception e) {
            log.error("error",e);
        }
        return restResult;
    }

}
